//
//  PublicSwiftTableView.swift
//  maosheng
//
//  Created by lxl on 2019/8/12.
//

import UIKit
typealias TableViewRefresh = (_ isHeader : Bool) -> Void

class PublicSwiftTableView: PublicTableView {
    var refreshList : TableViewRefresh?
    var pageIndex = 1
    var noDataView : NoDataView?

    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        tableView.snp.makeConstraints { (make) in
            make.top.left.right.bottom.equalTo(0).priority(200)
        }
        addTheRefresh()
        
    }
    func addTheRefresh() {
        tableView.backgroundColor = KYP_f2f2f2_COLOR

        addHeaderRefreshFinish { [weak self] in
            if self?.refreshList != nil{
                self?.pageIndex = 1
                self?.refreshList!(true)
            }
        }
        addFooterRefreshFinish { [weak self] in
            if self?.refreshList != nil{
                self?.refreshList!(false)
            }
        }
    }
    func setRefreshFooter(){
       
        
        tableView.reloadData()
        pageIndex = pageIndex + 1
        tableView.mj_footer.isHidden = dataSource.count < pageIndex * 10
        
    }
    func showTheNoDataView(title : String) -> Void {
        if noDataView != nil {
            noDataView?.isHidden = false
        }else{
            noDataView = NoDataView()
            tableView.addSubview(noDataView!)
            noDataView?.snp.makeConstraints({ (make) in
                make.top.equalTo(0)
                make.centerX.equalToSuperview()
            })
            noDataView?.noDataLab.text = title
        }
        self.bringSubviewToFront(noDataView!)
        
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
